package com.threeox.httplibrary.builder;

import android.net.Uri;

import com.threeox.httplibrary.request.GetRequest;
import com.threeox.httplibrary.request.RequestCall;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 *
 * @ClassName: GetBuilder
 *
 * @Description: Todo(Get请求方式构造器)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/21 11:06
 *
 * @version 1.0
 */
public class GetBuilder extends OkHttpRequestBuilder<GetBuilder> {

    @Override
    public RequestCall build() {
        if (params != null) {
            url = appendParams(url, params);
        }
        return new GetRequest(url, tag, params, headers,id).build();
    }

    protected String appendParams(String url, Map<String, Object> params) {
        if (url == null || params == null || params.isEmpty()) {
            return url;
        }
        Uri.Builder builder = Uri.parse(url).buildUpon();
        Set<String> keys = params.keySet();
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext())
        {
            String key = iterator.next();
            builder.appendQueryParameter(key, String.valueOf(params.get(key)));
        }
        return builder.build().toString();
    }

}
